﻿using ApiLib.Helpers;
using Furion.Templates;
using SqlSugar;

namespace ApiLib.AppStart;

/// <summary>
/// 控制台应用程序，详情：<see href="http://172.20.30.76:16003/zh/">ApiLib Docs</see>
/// </summary>
public static class ConsoleApp
{
    /// <summary>
    /// 数据库
    /// </summary>
    public static ISqlSugarClient Db => DbHelper.Db;

    /// <summary>
    /// 数据库
    /// </summary>
    public static SqlSugarScope? DbScope => DbHelper.DbScope;
    
    /// <summary>
    /// 启动
    /// </summary>
    /// <param name="config">配置对象</param>
    public static void Start(AppCoreConfig? config = null)
    {
        GlobalConfig.AppCoreConfig = config ?? new AppCoreConfig();
        Console.ForegroundColor = ConsoleColor.Green;
        Console.WriteLine(GlobalConfig.Banner);
        Console.ForegroundColor = ConsoleColor.Blue;

        // Serve.RunNative(RunOptions.Default);//启动web，有swagger，使用Startup
        //不启动WEB，但是会执行Startup
        Serve.RunNative(collection =>
        {
            //显示基础信息
            var appInfo = GlobalConfig.AppInfo;
            if (appInfo != null)
            {
                var template = TP.Wrapper(appInfo.Title, appInfo.Description,
                    $"##作者## {appInfo.Author}",
                    $"##当前版本## {appInfo.Version}",
                    $"##文档地址## {appInfo.Document}",
                    $"##Copyright## {appInfo.CompanyName}");
                Console.WriteLine(template);
                Console.ResetColor();
            }
        }, false);
    }
}